/*
 * Code based on FIR/IIR documentation found at:
 *
 * http://www-svr.eng.cam.ac.uk/~ajr/SpeechAnalysis/node13.html
 *
 */

#define FILTER_LENGTH 256
#define SIGNAL_LENGTH 1024

float input_data[SIGNAL_LENGTH];
float output_data[SIGNAL_LENGTH];
float filter1[FILTER_LENGTH];

/*
 *
 * * Applys an FIR filter to input to produce output. filter is
 *
 * * an array of coefficients to calculate a weighted sum of input
 *
 * * to get output. Note that for efficiency of calculation
 *
 * * the order of the terms in filter is the reverse of what
 *
 * * one might expect.
 *
 * *
 *
 * */

void applyFIR(float input[SIGNAL_LENGTH], float filter[FILTER_LENGTH], float output[SIGNAL_LENGTH]) {

	int i,j;

	for (i=0; i<FILTER_LENGTH; i++) {
		output[i] = 0.0;
		for (j=FILTER_LENGTH-i-1; j<=FILTER_LENGTH-1; j++) {
			output[i] = output[i] + input[1+i-FILTER_LENGTH+j]*filter[j];
		}
	}
	for (i=FILTER_LENGTH; i<SIGNAL_LENGTH; i++) {
		output[i] = 0.0;
		for (j=0; j<FILTER_LENGTH; j++) {
			output[i] = output[i] + input[1+i-FILTER_LENGTH+j]*filter[j];
		}
	}
}

int main() {

	int i;

	for (i=0; i<300; i++) {
		applyFIR(input_data, filter1, output_data);
	}
}
